package com.cauli.account.login.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.cauli.account.config.model.dto.ConfigDetailDTO;
import com.cauli.account.config.model.dto.ConfigQueryDTO;
import com.cauli.account.config.service.ConfigService;
import com.cauli.account.login.model.dto.LoginDTO;
import com.cauli.account.login.model.vo.LoginVerifyImgVO;
import com.cauli.account.login.model.dto.RegisterDTO;
import com.cauli.account.login.service.ImgVerifyCodeService;
import com.cauli.account.login.service.LoginService;
import com.cauli.account.login.model.vo.RouterVO;
import com.cauli.account.menu.service.MenuService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2022/12/12 16:27
 * @description 登录 前端控制器
 */
@Api(tags = "登录")
@ApiSort(1)
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private ImgVerifyCodeService imgVerifyCodeService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private MenuService menuService;

    public static final String CURRENT_MODULE = "登录";

    @ApiOperationSupport(order = 1)
    @ApiOperation("查询验证码是否开启")
    @GetMapping("/captchaMode")
    public ResultResponse<Boolean> loginVerifyMode() {
        ConfigQueryDTO configQueryDTO = new ConfigQueryDTO();
        configQueryDTO.setGroupName("login");
        ConfigDetailDTO configDetailDTO = configService.getConfig(configQueryDTO);
        return ResultResponse.success().data(configDetailDTO.getCaptchaMode());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("获取图形验证码")
    @GetMapping("/captcha")
    public ResultResponse<LoginVerifyImgVO> captcha() {
        return ResultResponse.success().data(imgVerifyCodeService.generatorCaptcha());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("注册")
    @PostMapping("/register")
    public ResultResponse<?> register(@Valid @RequestBody RegisterDTO registerDTO) {
        loginService.register(registerDTO);
        return ResultResponse.success().message("注册成功");
    }

    @MyLog(operation = "登录", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 4)
    @ApiOperation("登录")
    @PostMapping("/doLogin")
    public ResultResponse<?> doLogin(HttpServletRequest request, @Valid @RequestBody LoginDTO loginDTO) {
        SaTokenInfo login = loginService.login(request, loginDTO);
        if (login != null) {
            return ResultResponse.success().message("登录成功").data(login);
        } else {
            return ResultResponse.fail().message("登录失败");
        }
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("查询登录状态")
    @GetMapping("/isLogin")
    public ResultResponse<Boolean> isLogin() {
        return ResultResponse.success().data(StpUtil.isLogin());
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("查询Token信息")
    @GetMapping("/tokenInfo")
    public ResultResponse<SaTokenInfo> tokenInfo() {
        return ResultResponse.success().data(StpUtil.getTokenInfo());
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "注销")
    @PostMapping("/logout")
    public ResultResponse<?> logout() {
        StpUtil.logout();
        return ResultResponse.success().message("注销成功");
    }

    @SaCheckPermission("system:role:query")
    @ApiOperationSupport(order = 8)
    @ApiOperation("获取路由信息")
    @GetMapping("/getRouters")
    public ResultResponse<List<RouterVO>> getRouters() {
        Long loginIdAsLong = StpUtil.getLoginIdAsLong();
        List<RouterVO> routerList = menuService.getRouters(loginIdAsLong);
        return ResultResponse.success().data(routerList);
    }
}
